var Rotary = {};

(function()
{
	var elements = [];
	var elements_i = 0;
	var enable = true;
	Rotary.rotaryHandle = null;
	Rotary.init = function()
	{
		/*
		 *初始化旋钮
		 */
		/*if(Rotary.rotaryHandle||Rotary.rotaryHandle==0){
			log("gm.info.clearRotaryControl Rotary.rotaryHandle");
			gm.info.clearRotaryControl(Rotary.rotaryHandle); 	
		}
		
		log("gm.info.watchRotaryControl init");
		Rotary.rotaryHandle = gm.info.watchRotaryControl(function(status)
		{
			log("watchRoary status="+status);
			if (status == "RC_SELECT") {
				log("RC_SELECT");
				$(document).trigger('application.controls.pressed_select');
			}
			else if (status == "RC_CW" || status == "RC_RIGHT") {
				log("RC_CW");
				$(document).trigger('application.controls.pressed_right');
			}
			else if (status == "RC_CCW" || status == "RC_LEFT") {
				log("RC_CCW");
				$(document).trigger('application.controls.pressed_left');
			}
			else if (status == "RC_UP") {
				log("RC_UP");
				$(document).trigger('application.controls.pressed_up');
			}
			else if (status == "RC_DOWN") {
				log("RC_DOWN");
				$(document).trigger('application.controls.pressed_down');
			}
		},function(){
			log("watchRootary failure");
		});
		*/
	};
	Rotary.view = null;
	// 资源绑定
	Rotary.fresh = function(view)
	{
		log("Rotary.fresh");
		elements = [];
		Rotary.view = view;
		if(!view){
			//电台左选按钮
			elements.push($("#leftArrowBtn"));
			
			//debug 前翻页按钮
			elements.push($("#prevBtn"));
			
			//debug 后翻页按钮
			elements.push($("#nextBtn"));
			
			//电台选中按钮
			//elements.push($(".selectChannelBtn"));
			
			//收藏 按钮
			elements.push($("#favouriveBtn"));
			
			// 播放-暂停 按钮
			elements.push($("#playBtn"));

			// 下一首 - 按钮
			elements.push($("#nextSongBtn"));
			
			//电台右选按钮
			elements.push($("#rightArrowBtn"));
			
			Rotary.init();
		}else{
			switch(view){
				case "LOGOUT":
					elements.push($("#logoutBtn"));
					elements.push($("#cancelLogoutBtn"));
					
					//默认选择取消按钮
					elements_i=1;
					$(elements[elements_i]).trigger("rotaryFocus");
					break;
			}
		}
		
	};

	// 启用旋钮
	Rotary.enable = function()
	{
		enable = true;
	};

	// 禁用旋钮
	Rotary.disable = function()
	{
		enable = false;
	};

	// 下一个
	function hwNext()
	{
		log("Inside hwNext");

		if (!enable) {
			return;
		}
		if(Rotary.view){
			$(elements[elements_i]).trigger("rotaryBlur");

			elements_i++;

			if (elements_i >= elements.length) {
				elements_i = 0;
			}
		}else{
			//旋钮选择取消循环
			if(elements_i >= (elements.length-1)){
				elements_i = elements.length-1;
			}else{
				$(elements[elements_i]).trigger("rotaryBlur");
				elements_i++;
			}
		}
		$(elements[elements_i]).trigger("rotaryFocus");
	}

	// 上一个
	function hwPrevious()
	{
		log("hwPrevious");
		if(Rotary.view){
			$(elements[elements_i]).trigger("rotaryBlur");
			
			elements_i--;

			if (elements_i < 0) {

				elements_i = elements.length - 1;
			}
		}else{
			//旋钮选择取消循环
			//旋钮选择取消循环
			if(elements_i <= 0){
				elements_i = 0;
			}else{
				$(elements[elements_i]).trigger("rotaryBlur");
				elements_i--;
			}
		}
		$(elements[elements_i]).trigger("rotaryFocus");

	}
	
	Rotary.selected = null;
	Rotary.doubleCatch ={
		//intime : false,
		interval : null,
		start:function(callback){
			//var self =this;
			this.interval = setTimeout(function(){
				//self.intime = true;//超出双击时间
				Rotary.selected = null;
				log("Click");
				typeof callback ==="function"&&callback();
			},400);
		},
		clear:function(){
			clearInterval(this.interval);
			Rotary.selected = null;
		}
	};
	
	function hwSelect()
	{
		$(elements[elements_i]).trigger("rotarySelect");
		/*var callback = function(){$(elements[elements_i]).trigger("rotarySelect");};
		log("hw Select");
		if(!Rotary.selected&&Rotary.selected!=0){//Roaty.selected未赋值状态
			Rotary.selected = elements_i;	//赋值
			Rotary.doubleCatch.start(callback);	//开始计时
		}else{
			if(Rotary.selected!=elements_i){
				Rotary.selected = elements_i;
				Rotary.doubleCatch.start(callback);
			}else{
				//double click 触发
				DebugAlert("Double Click!!");
				Rotary.doubleCatch.clear();
			}
		}*/
	}

	function hwBack () {
		//var r = Backbone.history.fragment;
		log("Inside hwBack");
		//show logout pane
		$("#logout").removeClass("hidden");
		Rotary.fresh("LOGOUT");
	}

	$(document).on("application.controls.pressed_left", hwPrevious);
	$(document).on("application.controls.pressed_right", hwNext);
	$(document).on("application.controls.pressed_up", hwPrevious);
	$(document).on("application.controls.pressed_down", hwNext);
	$(document).on("application.controls.pressed_back", hwBack); 
	$(document).on("application.controls.pressed_select", hwSelect);

})();